using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_posts_editpostl : Controls.DataManagerPostControl
{
    private DAL.User CurrentUser
    {
        get
        {
            return Managers.UserLoginManager.CurrentUser;
        }
    }
    protected override void ItemChanged()
    {
        Refresh();
    }

    protected override void UserLoggedIn(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void UserLoggedOut(object sender, EventArgs e)
    {
        Refresh();
    }

    private void Refresh()
    {
        if (CurrentUser != null &&
            CurrentPost != null &&
            CurrentPost.Visible == true &&
            (CurrentPost.User == CurrentUser || CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator)))
        {
            if (CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
            {
                btnDelete.Visible = true;
            }
            else
            {
                btnDelete.Visible = false;
            }
            mviewPost.SetActiveView(viewPost);
            txtPost.Text = CurrentPost.Content;
            txtSubject.Text = CurrentPost.Subject;

            Header1.SetItem(CurrentPost);
            Header1.SubTitle = "Edit Post";
        }
        else
        {
            Header1.SetList();
            Header1.SetItem("Error occurred while editing post.");
            mviewPost.SetActiveView(viewNoThread);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CurrentPost = GetItemFromQueryString();
        }
        if (CurrentPost != null)
        {
            Page.Title = "FSTDT Forums - Edit Post";
        }
        else
        {
            Page.Title = "Post not found.";
        }
    }
    protected void valSubject_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (CurrentPost == CurrentPost.Thread.FirstPost)
        {
            if (String.IsNullOrEmpty(args.Value.Trim()))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CurrentPost.Subject = txtSubject.Text;
            CurrentPost.Content = txtPost.Text;
            DAL.Posts.Save(CurrentPost);

            Response.Redirect(String.Format("~/forums/thread.aspx?p={0}#{0}", CurrentPost.ID), true);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            CurrentPost.Visible = false;
            DAL.Posts.Save(CurrentPost);

            if (CurrentPost != CurrentPost.Thread.FirstPost)
            {
                CurrentPost.Thread.TotalReplies -= 1;
                CurrentPost.Thread.LastPost = DAL.Posts.GetLastPostByThread(CurrentPost.Thread);
                DAL.Threads.Save(CurrentPost.Thread);
            }

            mviewPost.SetActiveView(viewDeleted);
        }
    }
}
